Skip to content

Introduce UnaryOperatorTypeSpecifyingExtension interface#5284

Merged
ondrejmirtes merged 2 commits intophpstan:2.1.xfrom
Firehed:unary-operator-type-specifying-extension
Mar 24, 2026
Merged

Introduce UnaryOperatorTypeSpecifyingExtension interface#5284
ondrejmirtes merged 2 commits intophpstan:2.1.xfrom
Firehed:unary-operator-type-specifying-extension

Conversation

@Firehed
Copy link
Contributor

@Firehed Firehed commented Mar 23, 2026

Summary

  • Adds UnaryOperatorTypeSpecifyingExtension interface as the unary operator counterpart to the existing OperatorTypeSpecifyingExtension for binary operators
  • Allows extensions to customize the types returned by unary -, +, and ~ operators
  • Integrates with InitializerExprTypeResolver to call registered extensions before falling back to built-in type resolution

This PR extracts the infrastructure portion from #5223 to allow the API to be reviewed and merged independently before the GMP extension that uses it.

Test plan

  • Added UnaryOperatorTypeSpecifyingExtensionTypeInferenceTest with a synthetic test extension
  • Test extension verifies all three unary operators (-, +, ~) call extensions correctly
  • All existing tests pass

🤖 Generated with Claude Code

This adds an extension point for unary operators (-, +, ~), similar to the
existing OperatorTypeSpecifyingExtension for binary operators.

New types:
- UnaryOperatorTypeSpecifyingExtension interface
- UnaryOperatorTypeSpecifyingExtensionRegistry
- UnaryOperatorTypeSpecifyingExtensionRegistryProvider interface
- LazyUnaryOperatorTypeSpecifyingExtensionRegistryProvider implementation

Integration:
- Hook into InitializerExprTypeResolver for getUnaryPlusType, getUnaryMinusType,
  getBitwiseNotType
- Update UnaryPlusHandler to use InitializerExprTypeResolver (like UnaryMinusHandler)
- Add service tag: phpstan.broker.unaryOperatorTypeSpecifyingExtension

Testing:
- TestUnaryOperand fixture class
- TestUnaryOperatorTypeSpecifyingExtension synthetic extension
- UnaryOperatorTypeSpecifyingExtensionTypeInferenceTest

This enables extensions like GMP to specify return types for unary operators.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@VincentLanglet
Copy link
Contributor

@Firehed There is just the lint build to fix.

And since this is a new API, I prefer to get the approval from @ondrejmirtes too.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@Firehed
Copy link
Contributor Author

Firehed commented Mar 23, 2026

Sounds good, thanks! Hopefully this ought to be enough peeled out to finish the GMP work 🤞

@VincentLanglet VincentLanglet requested a review from staabm March 24, 2026 08:43
@ondrejmirtes ondrejmirtes merged commit 332e405 into phpstan:2.1.x Mar 24, 2026
651 checks passed
@ondrejmirtes
Copy link
Member

Thank you!

@Firehed Firehed deleted the unary-operator-type-specifying-extension branch March 24, 2026 16:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants